package com.app.shiyanbiji.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Repository;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * Description: MybatisInit
 * Author: hekezui
 */
@Repository
public class MybatisInit {

	@Resource
    private SqlSessionFactory sqlSessionFactory;

	@PostConstruct
    private void init() {
    	SqlSession session = null;
            try {
            	session = sqlSessionFactory.openSession();
                Connection conn = session.getConnection();
                Reader reader = Resources.getResourceAsReader("CreateDB.sql");
                ScriptRunner runner = new ScriptRunner(conn);
                runner.setLogWriter(null);
                runner.runScript(reader);
                reader.close();
            } catch (IOException e) {
				e.printStackTrace();
			} finally {
                if (session != null) {
                    session.close();
                }
            }
    }
}
